#include <func.h>
#include <time.h>

// 服务端

typedef struct 
{
    int fd;
    time_t time;
    bool alive;
} man;

void scuba_diving(int people, man c_fd[], int epoll_fd)
{   
    for(int i = 0; i < people; ++i)
        {
            time_t current_time = time(NULL);
            if(c_fd[i].alive == true && current_time - c_fd[i].time > 30)
            {
                epoll_ctl(epoll_fd, EPOLL_CTL_DEL, c_fd[i].fd, NULL);
                close(c_fd[i].fd);
                if(people > 1)
                {
                    c_fd[i] = c_fd[people - 1];
                    people--;
                }
                else
                    people--;
            }
        }
}

void transmit(int people, man c_fd[], char buf[], ssize_t ret, int current_fd)
{

                    for(int j = 0; j < people; ++j)
                    {
                        int other_fd = c_fd[j].fd;
                        if(other_fd == current_fd)
                        {
                            c_fd[j].time = time(NULL);
                            continue;
                        }
                        send(other_fd, buf, ret, 0);
                    }
}
int main(void)
{
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if(fd == -1)
        error(1, errno, "socket");

    struct sockaddr_in addr;
    addr.sin_port = htons(atoi("8080"));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    int reuse = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
    if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
        error(1, errno, "bind");

    if(listen(fd, 10) == -1)
        error(1, errno, "listen");

    int epoll_fd = epoll_create(1);
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = fd;
    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event);

    int people = 0;
    man c_fd[50];

    while(1)
    {
        struct epoll_event events[10];
        int count = epoll_wait(epoll_fd, events, 10, 1000);

        // 遍历就绪队列
        for(int i = 0; i < count; ++i)
        {
            int current_fd = events[i].data.fd;
            if(current_fd == fd)
            {
                c_fd[people].alive = true;
                c_fd[people].time = time(NULL);
                c_fd[people].fd = accept(fd, NULL, NULL);
                event.data.fd = c_fd[people].fd;
                epoll_ctl(epoll_fd, EPOLL_CTL_ADD, c_fd[people].fd, &event);
                people++;
            }
            else
            {
                char buf[128] = {0};
                int ret = recv(current_fd, buf, 128, 0);
                if(ret == 0)
                {
                    for(int i = 0; i < people; ++i)
                    {
                        if(c_fd[i].alive == true && c_fd[i].fd == current_fd)
                        {
                            epoll_ctl(epoll_fd, EPOLL_CTL_DEL, current_fd, NULL);
                            close(current_fd);
                            if(people > 1)
                            {
                                c_fd[i] = c_fd[people - 1];
                                people--;
                            }
                            else
                                people--;
                        }
                    }
                }
                else
                    transmit(people, c_fd, buf, ret, current_fd);
            }
        }
        // 检查是否有潜水者
        scuba_diving(people, c_fd, epoll_fd);
    }

    close(fd);
    return 0; 
}
